package com.learner.course.service.impl;

import com.learner.common.client.vod.VodMediaClient;
import com.learner.course.repository.CourseMediaRepository;
import com.learner.course.service.VideoService;
import com.learner.pojo.course.CourseMedia;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
* @author ice2020x
* @Date: 2023/3/8
* @Description: 视频逻辑类
**/
@Slf4j
@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    CourseMediaRepository courseMediaRepository;

    @Autowired
    private VodMediaClient vodMediaClient;

    @Override
    public CourseMedia save(CourseMedia courseMedia) {
        return courseMediaRepository.save(courseMedia);
    }

    @Override
    public CourseMedia getById(String id) {
        Optional<CourseMedia> optional = courseMediaRepository.findById(id);
        return optional.orElse(null);
    }

    @Override
    public void removeMediaVideoById(String id) {

        Optional<CourseMedia> optional = courseMediaRepository.findById(id);
        CourseMedia courseMedia = null;
        if (optional.isPresent()) {
            courseMedia = optional.get();
        }
        assert courseMedia != null;
        vodMediaClient.removeVideo(courseMedia.getVideoSourceId());
    }

    @Override
    public boolean removeById(String id) {
        courseMediaRepository.deleteById(id);
        return true;
    }

    @Override
    public CourseMedia findByPlanId(String planId) {
        return courseMediaRepository.findByPlanId(planId);
    }
}
